package com.blacktry.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.blacktry.entity.model.Admin;
import com.blacktry.entity.model.Menu;
import com.blacktry.mapper.MenuMapper;
import com.blacktry.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author blackTry
 * @since 2021-06-25
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<Menu> getMenusByAdminId() {
        Integer id = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
//        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
//        // 从redis中获取数据
//        List<Menu> menus = (List<Menu>) valueOperations.get("menu_" + id);
//        // 没有就从数据库中查
//        if (CollectionUtil.isEmpty(menus)) {
//            menus = this.baseMapper.getMenusByAdminId(id);
//            // 将数据设置到缓存中
//            valueOperations.set("menu_" + id, menus);
//        }
//        return menus;
        return this.baseMapper.getMenusByAdminId(id);
    }

    @Override
    public List<Menu> getMenusByRole() {
        return this.baseMapper.getMenusByRole();
    }

    @Override
    public List<Menu> getMenus() {
        return this.baseMapper.getMenus();
    }

}
